# 使用Ubuntu 22.04作为基础镜像
FROM docker.m.daocloud.io/ubuntu:22.04

# 设置非交互式安装，避免安装过程中的提示
ENV DEBIAN_FRONTEND=noninteractive

# 设置工作目录
WORKDIR /app
# 替换为清华源
RUN sed -i 's/archive.ubuntu.com/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list && \
  sed -i 's/security.ubuntu.com/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list && \
  apt-get update

# 更新包列表并安装基础依赖
RUN apt-get install -y \
    curl \
    wget \
    unzip \
    git \
    build-essential

# 安装Python 3.10+
RUN apt-get install -y python3.11 \
  python3-pip \
  python3.11-dev \
  python3-setuptools \
  && ln -sf /usr/bin/python3.11 /usr/bin/python3 \
  && ln -sf /usr/bin/python3.11 /usr/bin/python

RUN pip install -U auto-coder==2.0.16 --index-url https://pypi.tuna.tsinghua.edu.cn/simple

# 安装Node.js 22+
RUN curl -fsSL https://deb.nodesource.com/setup_22.x | bash - \
    && apt-get install -y nodejs

RUN rm -rf /var/lib/apt/lists/*

# 安装rust
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
ENV PATH="/root/.cargo/bin:${PATH}"

RUN git clone https://github.com/huggingface/tokenizers
WORKDIR /app/tokenizers/bindings/node
RUN npm install -g yarn
RUN yarn install && yarn run build
RUN ls -l

COPY app/ /app/
WORKDIR /app
RUN npm install -g pnpm
RUN pnpm install && \
    TOKENIZERS_DIR=$(node -p "require('path').dirname(require.resolve('tokenizers/package.json'))") && \
    find /app/tokenizers/bindings/node -name '*.node' -exec cp {} "${TOKENIZERS_DIR}/" \;

EXPOSE 7001

CMD ["node", "dist/main.js"]
